from django.conf import settings
from django.utils import timezone
from django.db import transaction
from django.db.models import Avg, Max, Min, Count, Sum
from django.forms.models import model_to_dict
from rest_framework import viewsets
from rest_framework.serializers import *
from rest_framework.response import Response
from rest_framework.validators import UniqueValidator, UniqueTogetherValidator  # 验证器
from datetime import datetime, timedelta
from pypinyin import pinyin, Style
import uuid

# from mysqldb.models import AllinpayAuthViews


def header_py_str(x=""):
    """返回简称"""
    vstr = ""
    obj = pinyin(x, style=Style.FIRST_LETTER)
    for i in obj:
        vstr += i[0]
    return vstr


def header_has_guuid():
    """生成唯一guuid"""
    guuid = uuid.uuid1()
    return guuid


def filter_views(obj):
    """筛选视图权限列表"""
    for sess in list(obj.keys()):
        if not isinstance(obj[sess], type):
            continue
        if not issubclass(obj[sess], viewsets.ModelViewSet):
            continue
        if not obj[sess].authentication_classes:
            continue
        # 判断视图验证类型
        idx = sum([classes.idx for classes in obj[sess].authentication_classes])
        # 写入数据库
        for act in obj[sess].http_method_names:
            name = obj[sess].__name__
            title = obj[sess].__title__
            group = obj[sess].__group__
            info = obj[sess].__info__.get(act)
            # AllinpayAuthViews.objects.update_or_create(
            #     defaults={"title": title, "group": group, "info": info, "idx": idx},
            #     name=name,
            #     act=act.upper(),
            # )
